package com.itheima.travel.config;


import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import com.itheima.travel.interceptor.PrimaryKeyInterceptor;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import java.util.Properties;

/*
声明配置类
读取外部配置文件
dao层接口映射扫描
* */
@Configuration
@PropertySource(value = "classpath:db.properties")
@MapperScan(
        basePackages = {"com.itheima.travel.mapper","com.itheima.travel.mapperExt"},
        sqlSessionFactoryRef = "sessionFactoryBean"
)
public class MybatisConfig {

    /*
    四大核心参数
    数据源配置
    数据源事务管理器
    sql会话工厂：
        数据源
        pojo类别名
        映射mapper.xml配置
        全文配置：
            驼峰、主键回填、缓存日志高级配置
    * */
    @Value("${dataSource.driverClassName}")
    private String driverClassName;
    @Value("${dataSource.url}")
    private String url;
    @Value("${dataSource.username}")
    private String username;
    @Value("${dataSource.password}")
    private String password;

    @Value("${seq.workerId}")
    private Integer workerId;

    @Value("${seq.datacenterId}")
    private Integer datacenterId;

    @Bean("dataSource")
    public DruidDataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();

        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

    @Bean("transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DruidDataSource dataSource){
        DataSourceTransactionManager transactionManager=new DataSourceTransactionManager(dataSource);
        return transactionManager;
    }

    @Bean("sessionFactoryBean")
    public SqlSessionFactoryBean sessionFactoryBean(@Qualifier("dataSource") DruidDataSource dataSource){
        SqlSessionFactoryBean sessionFactoryBean=new SqlSessionFactoryBean();

        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setTypeAliasesPackage("com.itheima.travel.pojo");
        /*try {
            sessionFactoryBean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("sqlMapper/*Mapper.xml")
            );
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setLogImpl(Log4j2Impl.class);
        sessionFactoryBean.setConfiguration(configuration);
        //插件：分页、主键
        sessionFactoryBean.setPlugins(new Interceptor[]{initPageInterceptor(),initPrimaryKeyInterceptor()});

        return sessionFactoryBean;
    }

    @Bean
    public PageInterceptor initPageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        pageInterceptor.setProperties(properties);
        return pageInterceptor;

    }

    @Bean
    public SnowflakeIdWorker snowflakeIdWorker(){
        return new SnowflakeIdWorker(workerId, datacenterId);
    }

    @Bean
    public PrimaryKeyInterceptor initPrimaryKeyInterceptor(){
        PrimaryKeyInterceptor primaryKeyInterceptor =  new PrimaryKeyInterceptor();

        Properties properties = new Properties();
        properties.setProperty("primaryKey", "id");

        primaryKeyInterceptor.setProperties(properties);
        primaryKeyInterceptor.setSnowflakeIdWorker(snowflakeIdWorker());

        return primaryKeyInterceptor;
    }
}
